package com.irdstudio.apicenter.gateway.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;



/*
 * 微信菜单配置
 * 
 * */

@Controller
public class WxGetResp implements IController {
	//先查
	private static final String getWxMenuResp1 = "select * from WX_RESP_ACTION where WX_RESP_ACTIONID=?";
	
	private static final String getWxMenuResp2 = "select * from WX_RESP_DETAIL where WX_RESP_ACTIONID=?";
	
	@Override
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket,
			HttpServletRequest request, HttpServletResponse response)
			throws MaccsException {
		
		String respActionid = inPacket.getValue("respActionid");
		
		// 获取用户数据权限
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		List<Map<String, Object>> wxRespList = jdbcDao.findMapList(getWxMenuResp1,
				new Object[] { respActionid });
		//只能查到一条数据，判断其类型，如果为文本，则直接取出文本值返回，如果为图文，则再用wxRespActionid去另一个表查
		
		//判断是否查到值
		if(wxRespList.size()==0||wxRespList==null){		//无结果
			respActionid = "defaultresp";
			wxRespList = jdbcDao.findMapList(getWxMenuResp1, new Object[] { respActionid });
		}
		
		//响应类型，0为纯文本，1为图文
		String respType = wxRespList.get(0).get("WX_RESP_TYPE").toString();
		
		
		MaccsPacket outPacket=null;
		if(respType.equalsIgnoreCase("1")){		//图文
			wxRespList = jdbcDao.findMapList(getWxMenuResp2,
					new Object[] { respActionid });
			outPacket = MaccsPacketUtil.makeSimpleSuccessPacket("OK!");
			outPacket.addVariable("wxRespList", wxRespList);
			outPacket.addVariable("resptyp", "news");
		}else{	//纯文本
			outPacket = MaccsPacketUtil.makeSimpleSuccessPacket("OK!");
			outPacket.addVariable("wxRespList", wxRespList);
			outPacket.addVariable("resptyp", "text");
		}
		return new JsonView(request, response, outPacket);
		
	}

}
